package com.epam.qanda.db.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.epam.qanda.db.ConnectionManager;
import com.epam.qanda.db.dao.BaseDao;
import com.epam.qanda.db.mapping.Converter;
import com.epam.qanda.db.model.UserAnswerLike;

public class UserAnswerLikeDao implements BaseDao {
	private final String ADD_USER_ANSWER_LIKE = "INSERT INTO user_answer_like (user_id, answer_id,direction) VALUES (?,?,?)";
	private final String REMOVE_USER_ANSWER_LIKE = "DELETE FROM user_answer_like WHERE id = ?";
	private final String GET_USER_ANSWER_LIKE_BY_ID = "SELECT * FROM user_answer_like WHERE id = ?";
	private final String UPDATE_USER_ANSWER_LIKE = "UPDATE user_answer_like SET user_id=?, answer_id=?, direction=? WHERE id = ?";
	private final String GET_USER_ANSWER = "SELECT * FROM user_answer_like WHERE user_id=? AND answer_id=?";
	private static final Logger LOG = Logger.getLogger("stdout");
	
	@Override
	public <T> void create(T element) {
		UserAnswerLike temp = (UserAnswerLike) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(ADD_USER_ANSWER_LIKE)) {
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getAnswerId());
			preparedStatement.setBoolean(3, temp.getDirection());
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> T read(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		UserAnswerLike userAnswerLike = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_USER_ANSWER_LIKE_BY_ID)) {
			statement.setInt(1, id);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				userAnswerLike = Converter
						.<UserAnswerLike> fromResaultSetToObject(rs,
								UserAnswerLike.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) userAnswerLike;
	}

	@Override
	public void delete(Integer id) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(REMOVE_USER_ANSWER_LIKE)) {
			preparedStatement.setInt(1, id);
			preparedStatement.executeUpdate();
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
	}

	@Override
	public <T> void update(T element) {
		UserAnswerLike temp = (UserAnswerLike) element;
		Connection connection = ConnectionManager.getInstance().getConnection();
		try (PreparedStatement preparedStatement = connection
				.prepareStatement(UPDATE_USER_ANSWER_LIKE)) {
			preparedStatement.setInt(4, temp.getId());
			preparedStatement.setInt(1, temp.getUserId());
			preparedStatement.setInt(2, temp.getAnswerId());
			preparedStatement.setBoolean(3, temp.getDirection());
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}

	}
	

	@SuppressWarnings("unchecked")
	public <T> T getUserAnswer(Integer userId, Integer answerId) {
		Connection connection = ConnectionManager.getInstance().getConnection();
		UserAnswerLike userAnswerLike = null;
		try (PreparedStatement statement = connection
				.prepareStatement(GET_USER_ANSWER)) {
			statement.setInt(1, userId);
			statement.setInt(2, answerId);
			ResultSet rs = statement.executeQuery();
			if (rs.next())
				userAnswerLike = Converter
						.<UserAnswerLike> fromResaultSetToObject(rs,
								UserAnswerLike.class);
		} catch (SQLException e) {
			LOG.warn("smth wrong " + e.getMessage());
		} finally {
			ConnectionManager.getInstance().release(connection);
		}
		return (T) userAnswerLike;
	}
}
